package com.company.test16;


import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * Created by Administrator on 2016.8.4.
 */
public class Test1610 extends JFrame {
    public Test1610(){
        MyPanel panel=new MyPanel();

        this.add(panel);
    }

    private class MyPanel extends JPanel{
        String word;

        public MyPanel(){
            word="";

            addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    super.keyPressed(e);

                    if (e.getKeyCode()==KeyEvent.VK_ENTER){
                        repaint();
                    }else if(Character.isLetterOrDigit(e.getKeyChar())){
                        word=word+e.getKeyChar();
                    }
                }
            });

            this.setFocusable(true);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.drawString(word,getWidth()/2,getHeight()/2);
            word="";
        }
    }
}
